package cn.jiyun.web.interceptor;

import cn.jiyun.dao.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从session中获取currentUser对应的用户信息
        User currentUser = ((User) request.getSession().getAttribute("currentUser"));
        // 如果信息存在表示已经登录，放行
        if (currentUser != null) {
            return true;
        }
        // 如果用户信息不存在，表示没有登录，重定向到登录界面
        response.sendRedirect("/loginPage");
        return false;
    }
}